<?php

namespace app\admin\controller;
class VisitSummary extends base
{
    //前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
        'echarts' => ['only' => 'index'],
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (!empty($data['cid'])) {
                $this->datas ['cid'] = $data['cid'];
            }
            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }
    }

    /**
     * 绘图
     */
    public function echarts()
    {
        switch ($this->request->param('type')) {
            case 'day':
                $today = $this->model->whereTime('create_time', 'today')->count('antepage');//今天
                $yesterday = $this->model->whereTime('create_time', 'yesterday')->count('id');//昨天
                $week = $this->model->whereTime('create_time', 'week')->count('id');//本周
                $lastweek = $this->model->whereTime('create_time', 'last week')->count('id');//上周
                $month = $this->model->whereTime('create_time', 'month')->count('id');//本月
                $lastmonth = $this->model->whereTime('create_time', 'last month')->count('id');//上月
//                $year = $this->model->whereTime('create_time', 'year')->count('id');//今年
//                $lastyear = $this->model->whereTime('create_time', 'last year')->count('id');//去年
                $data = [
                    ['value' => $today, 'name' => '今天'],
                    ['value' => $yesterday, 'name' => '昨天'],
                    ['value' => $week, 'name' => '本周'],
                    ['value' => $lastweek, 'name' => '上周'],
                    ['value' => $month, 'name' => '本月'],
                    ['value' => $lastmonth, 'name' => '上月'],
//                    ['value' => $year, 'name' => '今年'],
//                    ['value' => $lastyear, 'name' => '去年'],
                ];
                break;
            case 'browser':
                $data = $this->model->field('`browser` as `name` , count(ip) AS `value`')
                    ->group('browser')
                    ->where($this->datas)
                    ->order('value desc')
                    ->limit('8')
                    ->select();
                break;
            case 'browsertype':
                $data = $this->model->field('`browsertype` as `name` , count(ip) AS `value`')
                    ->group('browsertype')
                    ->where($this->datas)
                    ->order('value desc')
                    ->limit('8')
                    ->select();
                break;
            case 'antepage':
                $data = $this->model->field('`antepage` as `name` , count(ip) AS `value`')
                    ->group('antepage')
                    ->where($this->datas)
                    ->order('antepage desc')
                    ->limit('8')
                    ->select();
                break;
            case 'network':
                $data = $this->model->field('`network` as `name` , count(ip) AS `value`')
                    ->group('network')
                    ->where($this->datas)
                    ->order('network desc')
                    ->limit('8')
                    ->select();
                break;
            default:
                //查询一张表中某个字段数据重复次数最多的前5名
                //select `dizhi` as `name` , count(ip) AS `value` from tp_visit_summary  group by `ip` order by `value` desc
                $data = $this->model->field('`dizhi` as `name` , count(ip) AS `value`')
                    ->group('dizhi')
                    ->where($this->datas)
                    ->order('value desc')
                    ->limit('8')
                    ->select();
                break;
        }

        $echarts = json_encode($data);

        $this->assign('echarts', $echarts);
    }

        /**
     * 导出xls
     */
    public function exportSN()
    {
        header("Content-Type: text/html; charset=utf-8");
        header("Content-type:application/vnd.ms-execl");
        header("Content-Disposition:filename=huizong.xls");
        $arr = array(
            array('en' => 'ip', 'cn' => 'ip地址'), 
            array('en' => 'dizhi', 'cn' => '地址'), 
            array('en' => 'create_time', 'cn' => '时间'), 
        );
        $i = 0;
        $fieldCount = count($arr);
        $s = 0;
        foreach ($arr as $f) {
            if ($s < $fieldCount - 1) {
                echo iconv('utf-8', 'gbk', $f['cn']) . "\t";
            } else {
                echo iconv('utf-8', 'gbk', $f['cn']) . "\n";
            }
            $s++;
        }
        $db = db('VisitSummary');
        $sns = $db->order('id ASC')->column('id,create_time,dizhi,ip');
        if ($sns) {

            foreach ($sns as $sn) {
                $j = 0;
                foreach ($arr as $field) {
                    $fieldValue = $sn[$field['en']];
                    switch ($field['en']) {
                        default:
                            break;
                        case 'dizhi':
                            $fieldValue = iconv('utf-8', 'gbk', $fieldValue);
                            break;
                        case 'create_time':
                            $fieldValue = date('Y-m-d H:i:s',$fieldValue);
                            break;
                    }
                    if ($j < $fieldCount - 1) {
                        echo $fieldValue . "\t";
                    } else {
                        echo $fieldValue . "\n";
                    }
                    $j++;
                }
                $i++;
            }
        }
        exit();
    }

}
